#ifndef ABSTRACTMANAGE_H
#define ABSTRACTMANAGE_H

// C++ lib import
#include <functional>

// Qt lib import
#include <QObject>
#include <QSharedPointer>
#include <QPointer>
#include <QVector>
#include <QMap>
#include <QSet>
#include <QMutex>
#include <QHostAddress>

#include "Session.h"

class AbstractManage : public QObject
{
	Q_OBJECT

public:
	AbstractManage(const int &handleMaxThreadCount, QObject * parent = 0);

	~AbstractManage();

	inline void setHttpAcceptedCallback(const std::function< void(const QPointer< Session > &session) > &httpAcceptedCallback)
	{
		httpAcceptedCallback_ = httpAcceptedCallback;
	}

	virtual bool isRunning() = 0;

	public slots:
	bool begin();

	void close();

protected:
	virtual bool onStart() = 0;

	virtual void onFinish() = 0;

	bool startServerThread();

	void stopHandleThread();

	void stopServerThread();

	void newSession(const QPointer< Session > & session);

	void handleAccepted(const QPointer< Session > & session);

signals:
	void readyToClose();

protected:
	QSharedPointer< QThreadPool > handleThreadPool_;
	QSharedPointer< QThreadPool > serverThreadPool_;

	QMutex mutex_;

	std::function< void(const QPointer< Session > &session) > httpAcceptedCallback_;

	QSet< Session * > availableSessions_;
};

#endif // ABSTRACTMANAGE_H
